package com.budwk.app.sqfwV2.service.landscapePatrol;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.common.config.AppException;
import com.budwk.app.sqfw.constants.SqfwFormSummaryConstants;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwCompleteflowService;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.AbstractCustomerPdfCreateField;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowBusinessParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowPdfParamForController;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.pdfService.CompleteFlowCommonPdfService;
import com.budwk.app.sqfwDoc.codeGeneratorV2.result.GeneratorPdfWordResult;
import com.budwk.app.sqfwV2.dto.LandscapePatrolDTO;
import com.budwk.app.sqfwV2.dto.PropagandaScreeningDTO;
import com.budwk.app.sqfwV2.dto.dulaying.ScreeningHiddenDTO;
import com.budwk.app.sqfwV2.enums.AuthenticRightEnum;
import com.budwk.app.sqfwV2.enums.LandscapePatrolEnum;
import com.budwk.app.sqfwV2.enums.PatrolAndVisitEnum;
import com.budwk.app.sqfwV2.generator.baiyunqv.AuthenticRightPdfGenerator;
import com.budwk.app.sqfwV2.generator.landscapePatrol.LandscapePatrolPdfGenerator;
import com.budwk.app.sqfwV2.generator.patrolAndVisit.PatrolAndVisitPdfGenerator;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.budwk.starter.security.utils.SecurityUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@IocBean
public class LandscapePatrolPdfService extends CompleteFlowCommonPdfService {

    @Inject
    SqfwCompleteflowService sqfwCompleteflowService;

    @Override
    public void createPdfAndMsg(CompleteFlowPdfParamForController paramForController) {
        super.createPdfAndMsg(paramForController);
    }


    @Override
    public GeneratorPdfWordResult createPdfSync(CompleteFlowPdfParamForController paramForController) {
        return super.createPdfSync(paramForController);
    }

    /**
     * 获取用户自定义字段 代码生成器已经自动填充
     * 无需特殊修改
     *
     * @param paramData json字符串
     * @return 结果
     */
    @Override
    protected AbstractCustomerPdfCreateField getCustomerPdfField(String paramData) {
        return JSONObject.parseObject(paramData, LandscapePatrolDTO.class);
    }

    /**
     * 允许用户自定义填充在创建pdf时所提供的数据
     *
     * @param pdfGenerateParam 创建pdf所提供的数据
     */
    @Override
    protected void customerPopularPdfGenerateParam(PdfGenerateParam pdfGenerateParam) {


    }

    /**
     * 在这里设置生成pdf的所有generator
     *
     * @param pdfGenerateParam 创建pdf的参数
     * @return 生成pdf的generator合集
     */
    @Override
    protected List<BasePdfGenerator> getPdfGenerator(PdfGenerateParam pdfGenerateParam) {
        List<BasePdfGenerator> generatorList = Lists.newArrayList();
        CompleteFlowBusinessParam businessParam = (CompleteFlowBusinessParam) pdfGenerateParam.getBusinessParam();
        Sqfw_formSummary_new formSummary = businessParam.getFormSummary();
        LandscapePatrolDTO  landscapePatrolDTO = JSON.parseObject(formSummary.getParamData(), LandscapePatrolDTO.class);
        generatorList.add(new LandscapePatrolPdfGenerator(LandscapePatrolEnum.getPdfTemplatName(landscapePatrolDTO.getPatrolType())));
        return generatorList;
    }

    /**
     * 用户自定义下载pdf文件的名称
     *
     * @param pdfGenerateParam 创建pdf所提供的参数
     * @return 结果
     */
    @Override
    protected String getDownloadFileName(PdfGenerateParam pdfGenerateParam) {
        CompleteFlowBusinessParam businessParam = (CompleteFlowBusinessParam) pdfGenerateParam.getBusinessParam();
        Sqfw_formSummary_new formSummary = businessParam.getFormSummary();
        LandscapePatrolDTO landscapePatrolDTO = JSON.parseObject(formSummary.getParamData(), LandscapePatrolDTO.class);
        return StrUtil.format(LandscapePatrolEnum.getFileName(landscapePatrolDTO.getPatrolType()));
    }

    /**
     * 第五张表中的需要的类型，详情见 {@link SqfwFormSummaryConstants}
     *
     * @return 返回数据
     */
    @Override
    public String getSummaryType() {
        return SqfwFormSummaryConstants.forest_and_river_flood_disaster;
    }

    @Override
    protected void beforeInsertForSummary(Sqfw_formSummary_new formSummary) {
        // 参数校验
        checkParam(formSummary);
        // 入库前参数准备
        buildBeforeParam(formSummary, SecurityUtil.getUserId());
    }

    private void checkParam(Sqfw_formSummary_new formSummary) {
        if (formSummary == null) {
            throw new AppException("请求参数为空！");
        }
        if (StringUtils.isEmpty(formSummary.getParamData())) {
            throw new AppException("请求参数paramData为空！");
        }
    }

    private void buildBeforeParam(Sqfw_formSummary_new formSummary, String userId) {
        // 数据准备
        LandscapePatrolDTO pdfDTO = JSONObject.parseObject(formSummary.getParamData(), LandscapePatrolDTO.class);
        if (StringUtils.isEmpty(formSummary.getDetailAddress())) {
            SysUserService userService = NutzIocUtils.getBean(SysUserService.class);
            Sys_user user = userService.fetch(SecurityUtil.getUserId());
            String detailAddress = String.format("%s%s%s%s%s", com.budwk.app.util.StrUtil.formatStr(user.getProvince()),
                    com.budwk.app.util.StrUtil.formatStr(user.getCity()),
                    com.budwk.app.util.StrUtil.formatStr(user.getDistrict()),
                    com.budwk.app.util.StrUtil.formatStr(user.getCounty()),
                    com.budwk.app.util.StrUtil.formatStr(user.getCommunity()));
            formSummary.setDetailAddress(detailAddress);
        }
        LandscapePatrolDTO f = com.budwk.app.util.StrUtil.serializerData(LandscapePatrolDTO.class, pdfDTO);
        formSummary.setParamData(JSONObject.toJSONString(f));
    }

    @Override
    protected void customerPopularPdfGenerateParam(PdfGenerateParam pdfGenerateParam, Map<String, Object> customerBusinessDataMap) {
        // 获取新表数据
        Sqfw_formSummary_new formSummary = ((CompleteFlowBusinessParam) pdfGenerateParam.getBusinessParam()).getFormSummary();
        String paramData = formSummary.getParamData();
        // 数据前准备
        buildBeforeParam(formSummary, formSummary.getCreateBy());
        String paramData1 = formSummary.getParamData();
        if (!paramData1.equals(paramData)) {
            SqfwFormsummaryNewService sqfwFormsummaryNewService = NutzIocUtils.getBean(SqfwFormsummaryNewService.class);
            Sqfw_formSummary_new formSummaryNew = sqfwFormsummaryNewService.fetch(formSummary.getId());
            formSummaryNew.setParamData(paramData1);
            sqfwFormsummaryNewService.updateIgnoreNull(formSummaryNew);

            LandscapePatrolDTO roadPdfDTO = JSONObject.parseObject(paramData1, LandscapePatrolDTO.class);
            pdfGenerateParam.setAbstractCustomerPdfCreateField(roadPdfDTO);
        }
    }


}
